package com;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.icu.util.Calendar;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.widget.ImageView;
import com.dev.ErrorActivity;
import java.io.File;
import java.io.FileWriter;
import java.util.Map;
import android.widget.LinearLayout;
import com.dev.MainActivity;
import android.widget.TextView;
import com.dev.IconView;
public final class log {
    public static Context c;
    public static File logFile=new File(Environment.getExternalStorageDirectory(),"com.dev.log");
  	public static void alert(final String m){
		if(Looper.getMainLooper().isCurrentThread()){
			alertdo(m);
		}else{
			Handler h=new Handler(Looper.getMainLooper());
			h.post(new Runnable(){
				@Override
				public void run(){
					alertdo(m);
				}
			});
		}
	}
	public static long scheduled;
	public static void schedule(){
		scheduled=System.currentTimeMillis();
	}
	public static void time(String msg){
		log.alert(msg,"used time:"+(System.currentTimeMillis()-scheduled)+"ms");
		scheduled=System.currentTimeMillis();
	}
	static boolean single=true;
	public static void alertSingle(String cs){
		if(single){
			single=false;
			alert(cs);
		}
	}
	public static void alert(final String title,final Bitmap b){
		final AlertDialog.Builder ad=new AlertDialog.Builder(c);
		ad.setTitle(title);
		ImageView img=new ImageView(c);
		img.setImageBitmap(b);
		ad.setView(img);
		if(Looper.getMainLooper().isCurrentThread()){
			ad.show();
		}else{
			Handler h=new Handler(Looper.getMainLooper());
			h.post(new Runnable(){
				@Override
				public void run(){
					ad.show();
				}
			});
		}
	}
    public static void alert(final String t,final String m){
		if(Looper.getMainLooper().isCurrentThread()){
			alertdo(t,m);
		}else{
			Handler h=new Handler(Looper.getMainLooper());
			h.post(new Runnable(){
				@Override
				public void run(){
					alertdo(t,m);
				}
			});
		}
	}
    public static void log(String s){
        try{
			if(!logFile.exists()){
				logFile.createNewFile();
			}
            FileWriter fw=new FileWriter(logFile,true);
            fw.write(Calendar.getInstance().get(Calendar.HOUR_OF_DAY)+":"+Calendar.getInstance().get(Calendar.MINUTE)+"："+s+"\n");
            fw.close();
        }catch(Exception e){}
    }
	private static void alertdo(String s){
		AlertDialog.Builder ab=new AlertDialog.Builder(c);
		ab.setCancelable(false);
		ab.setNegativeButton("close",null);
		ab.setMessage(s).show();
	}
	private static void alertdo(String t,String m){
		AlertDialog.Builder ad=new AlertDialog.Builder(c);
		ad.setTitle(t);
		ad.setMessage(m);
		ad.show();
	}
	public static void threads(){
		Map<Thread,StackTraceElement[]> stes=Thread.getAllStackTraces();
		StringBuilder sb=new StringBuilder();
		for(Map.Entry<Thread,StackTraceElement[]> entry:stes.entrySet()){
			sb.append(entry.getKey().getName()+'\n');
		}
		log.alert(sb.toString());
	}
	public static void see(){
		class ExceptionInvigilator implements Thread.UncaughtExceptionHandler{
			@Override
			public void uncaughtException(Thread thread,Throwable throwable){
                if(throwable==null){
                    log.log("未知的异常");
                }else {
                    Intent intent=new Intent(c,ErrorActivity.class);
                    intent.putExtra("error",throwable.toString());
                    intent.putExtra("thread",thread.toString());
                    intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.addFlags(intent.FLAG_ACTIVITY_NO_ANIMATION);
                    c.startActivity(intent);
                    System.exit(0);
				}
			}
		}
		Thread.currentThread().setUncaughtExceptionHandler(new ExceptionInvigilator());
	}
    
}
